import 'package:flutter/material.dart';
import 'package:flutter_stort/html/integral_dao.dart';
import 'package:flutter_stort/model/integral_entity.dart';
import 'package:flutter_stort/widget/load_state_view.dart';

class IntegralPage extends StatefulWidget {
  @override
  _IntegralPageState createState() => _IntegralPageState();
}

class _IntegralPageState extends State<IntegralPage> {
  LoadState _loadState = LoadState.LOADING;
  int _integralNum = 0;
  List<IntegralEntity> _listData = [];

  @override
  void initState() {
    super.initState();
    IntegralDao.getIntegral().then((value) {
      _integralNum = value.integralNum;
      _listData.addAll(value.listData);
      _loadState = LoadState.SUCCESS;
      setState(() {});
    });
  }

  Widget _integralItemView(BuildContext context, int index) {
    return Container(
      height: 50,
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border(
          bottom: BorderSide(
            color: Colors.grey,
            width: 1,
            style: BorderStyle.solid,
          ),
        ),
      ),
      padding: EdgeInsets.all(10),
      child: Row(
        children: [
          Expanded(
            child: Text(
              _listData[index].title,
              style: TextStyle(
                color: Colors.black,
                fontSize: 18,
              ),
            ),
            flex: 1,
          ),
          Text(
            "${_listData[index].type == 1 ? "+1" : "-1"}",
            style: TextStyle(
              color: _listData[index].type == 1 ? Colors.blue : Colors.red,
              fontSize: 16,
            ),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "我的积分",
          style: TextStyle(
            fontSize: 20,
          ),
        ),
      ),
      body: LoadStateView(
        loadState: _loadState,
        erroRetry: () {},
        successWidget: Column(
          children: [
            Container(
              color: Colors.greenAccent,
              width: double.infinity,
              height: 140,
              alignment: Alignment.center,
              child: Text(
                "$_integralNum",
                style: TextStyle(
                  color: Colors.red,
                  fontSize: 40,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            Expanded(
              flex: 1,
              child: ListView.builder(
                itemBuilder: _integralItemView,
                itemCount: _listData.length,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
